package com.smarth.zhizulian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.smarth.zhizulian.R;
import com.smarth.zhizulian.app.BaseActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.utils.KumaLog;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.TimeUtils;
import com.smarth.zhizulian.view.HongBaoRainView;

import org.simple.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Created by 18865 on 2018/2/2.
 */

public class HongBaoRainActivity extends BaseActivity {
    @BindView(R.id.hb_rain)
    HongBaoRainView hbRain;
    @BindView(R.id.tv_lefttime)
    TextView tvLefttime;
    @BindView(R.id.tv_count)
    TextView tvCount;
    private Disposable mDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hongbaorain);
        ButterKnife.bind(this);
//        hbRain.start();
        initTimer();
        initHongBaoRain();
    }

    private void initHongBaoRain() {
        hbRain.setOnGetHongBao(new HongBaoRainView.OnGetHongBao() {
            @Override
            public void onGetHongBao(int count) {
                tvCount.setText("抓取红包：" + count + "个");
            }
        });
    }

    private void initTimer() {
        final long time = 10 * 1000;
        final long count = 10000l / 1000l;
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1) //设置循环
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(@NonNull Long aLong) throws Exception {
                        return time - (aLong * 1000); //
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//操作UI主要在UI线程
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        tvLefttime.setText(TimeUtils.getHongBaoRainTime(aLong));
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        KumaToast.showError(mContext, e.getMessage());
                    }

                    @Override
                    public void onComplete() {
//                                    onStartMyActivity();
                        EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.ON_FINISH_HONGBAORAIN, "HongBaoRainActivity", hbRain.getCount()), "HongBaoFragment");
                        HongBaoRainActivity.this.finish();
                    }
                });
    }

    public static void open(Context context) {
        Intent i = new Intent(context, HongBaoRainActivity.class);
        context.startActivity(i);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hbRain.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        hbRain.pause();
    }

    @Override
    protected void onDestroy() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            KumaLog.kuma("mDisposable");
            mDisposable.dispose();
        }
        hbRain.quit();
        super.onDestroy();
    }

    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .fitsSystemWindows(false)
                .init();
    }

}
